const BaseService = require("../base");

class CategoriesService extends BaseService {
  async getCategory1() {
    const { ctx } = this;

    const categories = await ctx.model.Product.Categories.find({
      level: 1,
    });
    // 返回数据
    return this.toId(categories, ["parentId", "level"]);
  }
  async getCategory2(category1Id) {
    const { ctx } = this;

    const categories = await ctx.model.Product.Categories.find({
      level: 2,
      parentId: category1Id,
    });
    // 返回数据
    return this.toId(categories, ["parentId", "level"]);
  }
  async getCategory3(category2Id) {
    const { ctx } = this;

    const categories = await ctx.model.Product.Categories.find({
      level: 3,
      parentId: category2Id,
    });
    // 返回数据
    return this.toId(categories, ["parentId", "level"]);
  }
  async saveCategory1(category) {
    const { ctx } = this;

    const result = await ctx.model.Product.Categories.create({
      ...category,
      level: 1,
    });
    // 返回数据
    return this.toId(result, ["parentId", "level"]);
  }
  async saveCategory2(category) {
    const { ctx } = this;

    const result = await ctx.model.Product.Categories.create({
      ...category,
      level: 2,
    });
    // 返回数据
    return this.toId(result, ["parentId", "level"]);
  }
  async saveCategory3(category) {
    const { ctx } = this;

    const result = await ctx.model.Product.Categories.create({
      ...category,
      level: 3,
    });
    // 返回数据
    return this.toId(result, ["parentId", "level"]);
  }
}

module.exports = CategoriesService;
